package tihi.com.horizotalscrolldemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import tihi.com.horizotalscrolldemo.base.HAdapter;

/**
 * Created by tihi on 2017/5/15.
 * 自定义界面的跟布局，监听touch事件， 通知界面的 HorizontalScrollView横向移动；
 */

public class HorizontalTestActivity extends Activity{

    private HorizontalScrollView mScrollView;
    private MyLinearLayout rootView;
    GridView gridView;
    ListView listView;

    private HAdapter<HorGridItem> gridViewAdapter;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, HorizontalTestActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hori_test_activity);
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void initView() {
        mScrollView = (HorizontalScrollView) findViewById(R.id.mScrollView);
        mScrollView.setHorizontalScrollBarEnabled(false);

        gridView = (GridView) findViewById(R.id.gridView);
        listView = (ListView) findViewById(R.id.listview);

        rootView = (MyLinearLayout) findViewById(R.id.rootView);
        rootView.setMoveListener(new MyLinearLayout.IMoveListener() {
            @Override
            public void onMove(int dx, int dy) {
                mScrollView.smoothScrollBy(dx, 0);
            }
        });

        gridViewAdapter = new HAdapter<HorGridItem>(this);
        gridView.setAdapter(gridViewAdapter);

        int itemSize = 6; //测试写死
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int itemWidth = metrics.widthPixels / 4;  //一屏显示4项
        gridView.setColumnWidth(itemWidth);
        gridView.setNumColumns(GridView.AUTO_FIT);

        int gridviewWidth = itemWidth * itemSize;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        gridView.setLayoutParams(params);
        gridView.setColumnWidth(itemWidth);
        gridView.setHorizontalSpacing(0);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(itemSize);

    }

    /**
     * 测试，写死6个数据
     */
    private void initData() {
        List<HorGridItem> items = new ArrayList<>();
        items.add(new HorGridItem(this, new HorData("风场1")));
        items.add(new HorGridItem(this, new HorData("风场2")));
        items.add(new HorGridItem(this, new HorData("风场3")));
        items.add(new HorGridItem(this, new HorData("风场4")));
        items.add(new HorGridItem(this, new HorData("风场5")));
        items.add(new HorGridItem(this, new HorData("风场6")));

        gridViewAdapter.setData(items);
    }
}
